
Page({
    data: {
         goodsList:[],
         allChoose:false,
         isTrue:false
        
    },
    onLoad:function(opt){
     
     
    },
    onShow:function(){
      var userInfo= wx.getStorageSync('userInfo');
        this.setData({
            userInfo:userInfo
      })
       this.getShopMarket();
    },
    getShopMarket:function(){
        var _self=this;
          wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/find?openid='+this.data.userInfo.openid,
            method:"GET",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
               if (res.data.code==200) {
                 _self.setData({
                    goodsList:res.data.obj
                 })
                 _self.chargeNum();
               }
                
                                      
             }
          }) 
    },
    selectCompony:function(e){
      console.log(e.currentTarget.dataset);
      console.log('***************');
      console.log(e.currentTarget.dataset.id);
      var arr=this.data.goodsList;
       for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log(arr.supplierCartVos[i]);
           if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.comid) {
               console.log('命中');
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
             
               arr.supplierCartVos[i].supplierCartParts[j].status=e.currentTarget.dataset.id
               
             }

           }
       }
    this.setData({
        goodsList:arr
     })
    
     this.getMoney(); 
    },
    chargeNum:function(){
      var arr=this.data.goodsList;
       for(var i=0;i<arr.supplierCartVos.length;i++){
           arr.supplierCartVos[i].allSecletc=true
           for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
            if (arr.supplierCartVos[i].supplierCartParts[j].status==0) {
               arr.supplierCartVos[i].allSecletc=false
            }  
           }
       }
       console.log('12312312321');
       console.log(arr);
       var allChoose=true;
       for (var i=0;i<arr.supplierCartVos.length;i++) {
         if (!arr.supplierCartVos[i].allSecletc) {
          allChoose=false;
         }
       }
      this.setData({
        goodsList:arr,
        allChoose:allChoose
      })
      this.fitGoods();
    },
    chooseSelect:function(e){
        console.log(e.currentTarget.dataset.item);//cartId
        var _self=this;
        wx.showModal({
        title: '提示',
        content: '是否删除'+e.currentTarget.dataset.item.name,
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            _self.delete(e.currentTarget.dataset.mid,e.currentTarget.dataset.item.cartId)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    },
    delete:function(mid,gid){
        var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/shopping/del',
            method:"post",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            data:{
              'openid':this.data.userInfo.openid, //公用请求头
              'merchantId':mid,
              'id':gid
            },
            success: function(res) {
              if (res.data.code==200) {
               _self.getShopMarket();
              }
            
            
         }
       })
    },
    chooseGoods:function(e){

     console.log(e.currentTarget.dataset.item);
     console.log(e.currentTarget.dataset.companyid);
     var arr=this.data.goodsList;
     for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log('merchantId='+arr.supplierCartVos[i].merchantId)
         if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.companyid) {
            console.log('命中1')
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
               
                if (arr.supplierCartVos[i].supplierCartParts[j].type==2) {
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id&&arr.supplierCartVos[i].supplierCartParts[j].normsId==e.currentTarget.dataset.item.normsId) {
                      arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=='0'?"1":"0"
                   } 
                }else{
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id) {
                      arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=='0'?"1":"0"
                   } 
                }

             
            }
         }
     }
     console.log(arr)
     this.setData({
        goodsList:arr
     })
    
     this.getMoney();
    },
    getMoney:function(){
            var _self=this;
            var arr=[];
            for(var i=0;i<this.data.goodsList.supplierCartVos.length;i++){
               for(var j=0;j<this.data.goodsList.supplierCartVos[i].supplierCartParts.length;j++){
                     var json={
                        "cartId":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].cartId,
                        "id":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].id,
                        "merchantId":this.data.goodsList.supplierCartVos[i].merchantId,//供应商ID:"GYS771337000452",
                        "type":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].type,//商品类型：1普通商品 2规格商品,
                        "normsId":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].normsId,//规格ID（如果type==1,就不需要传）,
                        "num":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].num,//数量,
                        "status":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].status//勾选状态：0没勾选 1已勾选
                    }
                   arr.push(json) 
               }  
            }
            console.log(arr)
            wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/countCart',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                pjCountCartRequestListVo:arr
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                if (res.data.code==200) {
                 _self.setData({
                    goodsList:res.data.obj
                 })
                 _self.chargeNum();
               }
                               
            }
            }) 
    },
    fitGoods:function(){
           var arr=this.data.goodsList;
           var isTrue=false;
           for(var i=0;i<arr.supplierCartVos.length;i++){
                  for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
                     
                    if(arr.supplierCartVos[i].supplierCartParts[j].status==1){
                      isTrue=true
                    }
                   
                  }
               
           }
           this.setData({
            isTrue:isTrue
           })
    },
    turn:function(){
    
     if(this.data.isTrue){
        wx.navigateTo({
          url: '/pages/shopMarketMake/shopMarket?type=hide'
        })
     }else{
         wx.showToast({
              title: '最少勾选一个商品',
              icon:'none',
              duration: 2000
            })
     }          
    },
    changeNum:function(e){
      console.log(e.detail.value)
      console.log(e.currentTarget.dataset.item.id)
      var arr=this.data.goodsList;
      for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log('merchantId='+arr.supplierCartVos[i].merchantId)
         if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.companyid) {
            console.log('命中1')
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
               
                if (arr.supplierCartVos[i].supplierCartParts[j].type==2) {
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id&&arr.supplierCartVos[i].supplierCartParts[j].normsId==e.currentTarget.dataset.item.normsId) {
                      arr.supplierCartVos[i].supplierCartParts[j].num=e.detail.value
                   } 
                }else{
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id) {
                      arr.supplierCartVos[i].supplierCartParts[j].num=e.detail.value
                   } 
                }

             
            }
         }
    }
     console.log(arr)
     this.setData({
        goodsList:arr
     })
     this.getMoney();
  },
    addAction:function(e){
      var arr=this.data.goodsList;
     for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log('merchantId='+arr.supplierCartVos[i].merchantId)
         if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.companyid) {
            console.log('命中1')
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
               
                if (arr.supplierCartVos[i].supplierCartParts[j].type==2) {
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id&&arr.supplierCartVos[i].supplierCartParts[j].normsId==e.currentTarget.dataset.item.normsId) {
                      arr.supplierCartVos[i].supplierCartParts[j].num++
                   } 
                }else{
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id) {
                      arr.supplierCartVos[i].supplierCartParts[j].num++
                   } 
                }

             
            }
         }
     }
     console.log(arr)
     this.setData({
        goodsList:arr
     })
     this.getMoney();
    },
    subAction:function(e){
       var arr=this.data.goodsList;
     for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log('merchantId='+arr.supplierCartVos[i].merchantId)
         if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.companyid) {
            console.log('命中1')
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
               
                if (arr.supplierCartVos[i].supplierCartParts[j].type==2) {
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id&&arr.supplierCartVos[i].supplierCartParts[j].normsId==e.currentTarget.dataset.item.normsId) {
                      if (arr.supplierCartVos[i].supplierCartParts[j].num>1) {
                        arr.supplierCartVos[i].supplierCartParts[j].num--
                      }else{
                        arr.supplierCartVos[i].supplierCartParts[j].num=1
                      }
                      

                   } 
                }else{
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id) {
                     if (arr.supplierCartVos[i].supplierCartParts[j].num>1) {
                        arr.supplierCartVos[i].supplierCartParts[j].num--
                      }else{
                        arr.supplierCartVos[i].supplierCartParts[j].num=1
                      }
                   } 
                }

             
            }
         }
     }
     console.log(arr)
     this.setData({ 
        goodsList:arr
     })
     this.getMoney();
    },
   
    allectAction:function(){
     var arr=this.data.goodsList;
     for(var i=0;i<arr.supplierCartVos.length;i++){
        
         for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
            arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=this.data.allChoose?"0":"1"
      
         }
     }
     console.log(arr)
     this.setData({
        goodsList:arr,
        allChoose:!this.data.allChoose
     })
     this.getMoney();
    },
    turnActivity:function(e){
     console.log(e.currentTarget.dataset.code)
      wx.navigateTo({
        url: '/pages/shopActivity/shopActivity?code='+e.currentTarget.dataset.code
      })


    }
});